package org.apache.struts2.gae.dispatcher.ng;

import org.apache.struts2.dispatcher.Dispatcher;
import org.apache.struts2.dispatcher.ng.HostConfig;
import org.apache.struts2.dispatcher.ng.InitOperations;
import org.apache.struts2.gae.dispatcher.GaeDispatcher;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * User: mikom
 * Date: 11/13/12
 * Time: 3:05 PM
 */
public class GaeInitOperations extends InitOperations {

    /**
     * Creates and initializes the dispatcher
     */
    @Override
    public Dispatcher initDispatcher( HostConfig filterConfig ) {
        Dispatcher dispatcher = createGaeDispatcher(filterConfig);
        dispatcher.init();
        return dispatcher;
    }

    public Dispatcher createGaeDispatcher(HostConfig filterConfig){
        Map<String, String> params = new HashMap<String, String>();
        for ( Iterator e = filterConfig.getInitParameterNames(); e.hasNext(); ) {
            String name = (String) e.next();
            String value = filterConfig.getInitParameter(name);
            params.put(name, value);
        }
        return new GaeDispatcher(filterConfig.getServletContext(), params);
    }
}
